//
//  DeatilViewController.m
//  WYModel
//
//  Created by ios on 2017/3/2.
//  Copyright © 2017年 Wy. All rights reserved.
//



#import "DeatilViewController.h"
#import "TypeScrollView.h"
#import "HomeViewController.h"

@interface DeatilViewController ()<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic,strong) UITableView *wTableView;
@property(nonatomic,strong) UIImageView *headerImgView;
@property(nonatomic,strong) TypeScrollView *typeScrollerView;

@property(nonatomic,strong) TypeScrollView *typeTopView;
@end

@implementation DeatilViewController


-(void)loadView{
    [super loadView];
    
   // [self.navigationController.navigationBar setShadowImage:[UIImage imageFromColor:[UIColor clearColor]]];
    
   // [[UIApplication sharedApplication].keyWindow addSubview:self.headerImgView];
   // [self.navigationController.view addSubview:self.headerImgView];
    
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    [self scrollViewDidScroll:self.wTableView];
    [self.wTableView reloadData];
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    //http://bxb.inszb.com:8082/index.php?g=api&m=user&a=get_time
//    [[WYNetWorking shareInstance] Get:@"http://bxb.inszb.com:8082/index.php?g=api&m=user&a=get_time" pargmeter:nil success:^(id responseObject) {
//        
//    } failure:^(NSError *error) {
//        
//    }];
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    UIView *titleView = [[UIView alloc] init];
    self.navigationItem.titleView = titleView;
    
    self.headerImgView.layer.cornerRadius = 30;
    self.headerImgView.layer.masksToBounds = YES;
    self.headerImgView.frame = CGRectMake(0, 0, 60, 60);
    self.headerImgView.backgroundColor = [UIColor redColor];
    // 保证用户头像水平居中
    self.headerImgView.center = CGPointMake(titleView.center.x, 0);
    [titleView addSubview:self.headerImgView];
    
   // [self.wTableView addSubview:self.typeTopView];
    
    
}


-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    
    [self.headerImgView removeFromSuperview];
}

-(void)typeBtnClickDelegateIndex:(NSInteger)index title:(NSString *)btnTitle{
    
    WYLog(@"选中类型--->%@-----%ld",btnTitle,index);
    
}


-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    
    CGFloat offsetY = scrollView.contentOffset.y + scrollView.contentInset.top;
    CGFloat scale = 1.0;
    // 放大
    if (offsetY < 0) {
        scale = MIN(1.5, 1 - offsetY / 300);
    } else if (offsetY > 0) {
        // 缩小
        scale = MAX(0.45, 1 - offsetY / 300);
    }
    //进行缩放
    self.headerImgView.transform = CGAffineTransformMakeScale(scale, scale);
    // 保证缩放后y坐标不变
    CGRect frame = self.headerImgView.frame;
    frame.origin.y = -self.headerImgView.layer.cornerRadius / 2;
    self.headerImgView.frame = frame;
    
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 80;
    
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString *cellID = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (cell ==nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
    }
    cell.backgroundColor = [UIColor purpleColor];
    cell.textLabel.text  = [NSString stringWithFormat:@"%ld",indexPath.row];
    return cell;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    return self.typeTopView;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return kWidth/5;
}

-(UITableView *)wTableView{
    if (!_wTableView) {
        _wTableView             = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, kWidth, kHeight) style:UITableViewStylePlain];
        _wTableView.delegate    = self;
        _wTableView.dataSource  = self;
        _wTableView.tableHeaderView = self.typeScrollerView;
        
       // [[UIApplication sharedApplication].keyWindow addSubview:self.typeTopView];
        
        [self.view addSubview:_wTableView];
        
    }
    return _wTableView;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    [self.navigationController pushViewController:[[HomeViewController alloc] init] animated:NO];
    
}

-(UIImageView *)headerImgView{
    if (!_headerImgView) {
        CGFloat imgW = 75;
        CGFloat imgH = 75;
        _headerImgView  = [[UIImageView alloc] initWithFrame:CGRectMake((kWidth-imgW)/2, 25, imgW, imgH)];
        _headerImgView.image = [UIImage imageNamed:@"longma2.jpg"];
        
    }
    
    return _headerImgView;
}

-(TypeScrollView *)typeScrollerView{
    if (!_typeScrollerView) {
        
        _typeScrollerView = [[TypeScrollView alloc] initWithFrame:CGRectMake(0, 0, kWidth, kWidth/5)];
        
        [_typeScrollerView addItemData:@[@"推荐",@"健身",@"职场",@"母婴",@"时尚"] andImageData:@[@"type-1",@"type-2",@"type-3",@"type-4",@"type-5"]];
        
        _typeScrollerView.block = ^(NSString *title,NSInteger index){
            WYLog(@"show%@----%ld",title,index);
            
        };
    }
    return _typeScrollerView;
}

-(TypeScrollView *)typeTopView{
    if (!_typeTopView) {
        
        _typeTopView = [[TypeScrollView alloc] initWithFrame:CGRectMake(0, 0, kWidth, kWidth/5)];
        
        [_typeTopView addItemData:@[@"推荐",@"健身",@"职场",@"母婴",@"时尚"] andImageData:@[@"type-1",@"type-2",@"type-3",@"type-4",@"type-5"]];
        
        _typeTopView.block = ^(NSString *title,NSInteger index){
            
            WYLog(@"hiden%@----%ld",title,index);
        };
        
    }
    return _typeTopView;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
